﻿using System.Net;
using System.Reflection;
using System.Text;
using System.Text.Json.Serialization;

namespace FanaticRock.MediaSource.BilibiliAPI.Helpers
{
    public static class ParameterHelper
    {
        public static string GetQureyParameter(object obj)
        {
            var type = obj.GetType();
            var sb = new StringBuilder();
            var counter = 0;
            foreach (var prop in type.GetProperties())
            {
                var jsonIgnore = prop.GetCustomAttribute<JsonIgnoreAttribute>();
                if (jsonIgnore != null) continue;

                var jsonPropertyName = prop.GetCustomAttribute<JsonPropertyNameAttribute>();
                if (jsonPropertyName == null) continue;

                var name = jsonPropertyName.Name;

                var value = prop.GetValue(obj, null);
                if (value == null) continue;

                var valueStr = string.Empty;

                if (prop.PropertyType.IsEnum)
                {
                    var enumValue = (int)value;
                    valueStr = enumValue.ToString();
                }
                else if (prop.PropertyType == typeof(string))
                {
                    var strValue = value.ToString();
                    valueStr = WebUtility.UrlEncode(strValue);
                }
                else
                {
                    valueStr = value.ToString();
                }

                if (counter != 0)
                    sb.Append("&");
                sb.Append($"{name}={valueStr}");

                counter++;
            }

            return sb.ToString();

        }

        public static async Task<string> GetWbiQureyParameter(object obj)
        {
            var type = obj.GetType();
            var dic = new Dictionary<string, string>();

            foreach (var prop in type.GetProperties())
            {
                var jsonIgnore = prop.GetCustomAttribute<JsonIgnoreAttribute>();
                if (jsonIgnore != null) continue;

                var jsonPropertyName = prop.GetCustomAttribute<JsonPropertyNameAttribute>();
                if (jsonPropertyName == null) continue;

                var name = jsonPropertyName.Name;

                var value = prop.GetValue(obj, null);
                if (value == null) continue;

                var valueStr = string.Empty;

                if (prop.PropertyType.IsEnum)
                {
                    var enumValue = (int)value;
                    valueStr = enumValue.ToString();
                }
                else if (prop.PropertyType == typeof(string))
                {
                    var strValue = value.ToString();
                    valueStr = WebUtility.UrlEncode(strValue);
                }
                else
                {
                    valueStr = value.ToString();
                }

                dic.Add(name, valueStr);

            }

            var wbi = await WbiHelper.GetWbiKeys();
            var dic2 = WbiHelper.EncWbi(dic, wbi);
            return string.Join('&', dic2.Select(kv => $"{kv.Key}={kv.Value}"));
        }

        public static async Task<string> GetQureyParameterFromMap(Dictionary<string,string> map)
        {
            return string.Join('&', map.Select(kv => $"{kv.Key}={kv.Value}"));
        }

    }
}
